package com.woniu.project.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.project.core.domain.Shop;
import com.woniu.project.core.domain.ShopStore;
import com.woniu.project.core.domain.Store;
import com.woniu.project.core.exception.ProjectAuthenException;
import com.woniu.project.core.exception.ProjectExceptionCode;
import com.woniu.project.dao.ShopMapper;
import com.woniu.project.dao.ShopStoreMapper;
import com.woniu.project.dao.StoreMapper;
import com.woniu.project.dto.StoreDto;
import com.woniu.project.dto.StoreManagementDto;
import com.woniu.project.params.SelectParam;
import com.woniu.project.params.StoreParams;
import com.woniu.project.service.StoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Klaus
 * @since 2020-10-15
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements StoreService {

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private ShopStoreMapper shopStoreMapper;

    @Autowired
    private ShopMapper shopMapper;


    /**
     * 分页查询所有的仓库
     * page:页数 默认值 1
     * size:每页条数 默认值 10
     * @return
     */
    @Override
    public IPage<StoreDto> selectAllStore(SelectParam selectParam) {
        Page<Store> page = new Page<>(selectParam.getPage(), selectParam.getSize());

        //条件构造器
        QueryWrapper<Store> wrapper = new QueryWrapper<Store>();
        IPage ipage = page(page, wrapper);

        //将Store转为StoreDto
        List<Store> records = ipage.getRecords();
        List<StoreDto> storeDtos = new ArrayList<>();
        for (Store record : records) {
            List<Shop> shops = new ArrayList<>();
            StoreDto storeDto = new StoreDto();
            BeanUtils.copyProperties(record,storeDto);
            List<ShopStore> shopStores = shopStoreMapper.selectShopStoreByStoreId(storeDto.getStoreId());
            for (ShopStore shopStore : shopStores) {
                Integer shopId = shopStore.getShopId();
                Shop shop = shopMapper.selectById(shopId);
                shops.add(shop);
            }
            storeDto.setShops(shops);
            storeDtos.add(storeDto);
        }
        ipage.setRecords(storeDtos);
        return ipage;
    }


    /**
     * 按条件查询仓库(分页)
     * page:页数 默认值 1
     * size:每页条数 默认值 10
     * StoreParams:仓库名称或联系人名称或联系人手机号
     * StoreStatus:仓库状态 使用/停用
     *
     * @param selectParam
     * @return
     */
    @Override
    public IPage<StoreManagementDto> selectStoreByCondition(SelectParam selectParam) {
        Page<StoreManagementDto> page = new Page<>(selectParam.getPage(), selectParam.getSize());

        IPage storeIPage = storeMapper.selectStoreByCondition(page, selectParam.getStoreParams(), selectParam.getStoreStatus());

        List<StoreManagementDto> records = storeIPage.getRecords();
        for (StoreManagementDto record : records) {
            List<Shop> shops = new ArrayList<>();
            //根据仓库ID查询门店和仓库的中间表
            List<ShopStore> shopStores = shopStoreMapper.selectShopStoreByStoreId(record.getStoreId());
            //根据中间表中的门店ID 去查询出对应的门店信息，并将信息存储到对应的list集合中
            for (ShopStore shopStore : shopStores) {
                Integer shopId = shopStore.getShopId();
                Shop shop = shopMapper.selectById(shopId);
                shops.add(shop);
            }
            record.setShops(shops);
        }
        storeIPage.setRecords(records);
        return storeIPage;
    }

    /**
     * 根据ID查询仓库
     *
     * @param storeId
     * @return
     */
    @Override
    public StoreDto selectStoreById(String storeId) {
        Store store = storeMapper.selectById(storeId);
        //根据仓库ID查询门店和仓库的中间表
        List<ShopStore> shopStores = shopStoreMapper.selectShopStoreByStoreId(store.getStoreId());
        List<Shop> shops = new ArrayList<>();
        for (ShopStore shopStore : shopStores) {
            Integer shopId = shopStore.getShopId();
            Shop shop = shopMapper.selectById(shopId);
            shops.add(shop);
        }
        StoreDto storeDto = new StoreDto();
        BeanUtils.copyProperties(store,storeDto);
        storeDto.setShops(shops);

        return storeDto;
    }

    /**
     * 新增门店信息
     * 必填参数：
     * StoreName：仓库名称
     * managementid ：仓库管理员的ID
     * StoreStatus ：仓库状态
     * StoreAddr：仓库地址
     *
     * @param storeParams
     * @return
     */
    @Override
    public void insertStore(StoreParams storeParams) {
        //根据仓库名称查询是否已经存在该仓库
        Store store = storeMapper.selectStoreByName(storeParams.getStoreName());
        if (store != null)
            throw new ProjectAuthenException(ProjectExceptionCode.STORE_REGISTERED);

        //如果不存在就进行新增
        Store store1 = new Store();
        BeanUtils.copyProperties(storeParams,store1);
        storeMapper.insert(store1);
    }

    /**
     * 修改仓库信息
     * 必填参数：
     * storeId:仓库编号
     * StoreName：仓库名称
     * managementid ：仓库管理员的ID
     * StoreStatus ：仓库状态
     * StoreAddr：仓库地址
     *
     * @param storeParams
     * @return
     */
    @Override
    public void updateStoreById(StoreParams storeParams) {
        //根据仓库ID查询该仓库是否存在
        Store store = storeMapper.selectById(storeParams.getStoreId());
        if (store == null)
            throw new ProjectAuthenException(ProjectExceptionCode.STORE_NOTFOUND);

        //如果仓库存在，就进行更新操作
        BeanUtils.copyProperties(storeParams,store);
        storeMapper.updateById(store);
    }

    /**
     * 按仓库ID删除仓库
     * @param storeId
     * @return
     */
    @Override
    public void deleteStoreById(String storeId) {
        //根据仓库ID查询该仓库是否存在
        Store store = storeMapper.selectById(storeId);
        if (store == null)
            throw new ProjectAuthenException(ProjectExceptionCode.STORE_NOTFOUND);

        //如果仓库存在就进行删除
        //先删除仓库表
        storeMapper.deleteById(storeId);
        //在删除仓库门店中间表
        shopStoreMapper.deleteShopStoreByStoreId(storeId);
    }

}
